<?php

use think\migration\Migrator;
use app\model\PageSetting;

class AlterPageSettingChangeExtData extends Migrator
{
   public function up()
   {
       $travels = [
           1   => 42,
           4   => 49,
           99  => 47,
           2   => 43,
           3   => 44,
           5   => 67,
           87  => 87,
       ];
       $foods = [
           'kfc'        => 38,
           'mcdonald'   => 26,
           'pagoda'     => 31,
           'spk'        => 34,
           'heytea'     => 37,
           'burgerking' => 46,
           'nayuki'     => 32,
           'luckin'     => 33,
       ];
       $affectedNum = 0;
       PageSetting::query()->whereIn('type', [PageSetting::TYPE_TACKOUT, PageSetting::TYPE_LOCAL_LIFE])
           ->where('status', PageSetting::STATUS_ACTIVE)
           ->whereLike('ext_data', '%"jump_type": 9}%')
           ->select()->each(function (PageSetting $pageSetting) use ($travels, $foods, &$affectedNum){
               $new_ext_data = $ext_data = $pageSetting->ext_data;
               if (isset($ext_data['path']) && !empty($path = $ext_data['path'])) {
                   $jump_type = $ext_data['jump_type'];
                   if ($jump_type == PRODUCT_TRAVEL_OIL_SERV) {
                       $pathArr = explode('id=', $path);
                       if ($pathArr && count($pathArr) > 1) {
                           $id = $pathArr[1];
                           if(isset($travels[$id]) && !empty($travels[$id])) {
                               $act_id = $travels[$id];
                               $new_ext_data['path'] = $path.'&act_id='.$act_id;
                               $pageSetting->ext_data = $new_ext_data;
                               $pageSetting->save();
                               $affectedNum++;
                           }
                       }
                   }
               }
           });
       PageSetting::query()->whereIn('type', [PageSetting::TYPE_TACKOUT, PageSetting::TYPE_LOCAL_LIFE])
           ->where('status', PageSetting::STATUS_ACTIVE)
           ->whereLike('ext_data', '%"jump_type": 11}%')
           ->select()->each(function (PageSetting $pageSetting) use ($travels, $foods, &$affectedNum){
               $new_ext_data = $ext_data = $pageSetting->ext_data;
               if (isset($ext_data['path']) && !empty($path = $ext_data['path'])) {
                   $jump_type = $ext_data['jump_type'];
                   if ($jump_type == PRODUCT_FOOD_SHOP_SERV) {
                       $pathArr = explode('product_type=', $path);
                       if ($pathArr && count($pathArr) >1) {
                           $product_type = $pathArr[1];
                           if(isset($foods[$product_type]) && !empty($foods[$product_type])) {
                               $act_id = $foods[$product_type];
                               $new_ext_data['path'] = $path.'&act_id='.$act_id;
                               $pageSetting->ext_data = $new_ext_data;
                               $pageSetting->save();
                               $affectedNum++;
                           }
                       }
                   }
               }
           });
       echo '影响行数:'.$affectedNum;
   }
}
